﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class BookingNote_ClientBookingNoteList : System.Web.UI.Page
{
    DataBase db = new DataBase();
    IBNBookingNoteHandler bookNoteManage = new BNBookingNoteHandler();
    ISyClientHandler clientManage = new SyClientHandler();
    IRolePurviewHandler userManage = new RolePurviewHandler();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1059")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }

            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "desc";
            dataBind();
        }
    }

    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";

        list.Add(new SqlParameter("@ShipperID", Session["LoginUserClientID"].ToString()));
        sqlstr += " and ShipperID=@ShipperID ";

        if (ViewState["PONO"] != null)
        {
            list.Add(new SqlParameter("@PONO", ViewState["PONO"].ToString()));
            sqlstr += " and PONO like '%' +  @PONO + '%'";
        }

        if (ViewState["InvoiceNO"] != null)
        {
            list.Add(new SqlParameter("@InvoiceNO", ViewState["InvoiceNO"]));
            sqlstr += " and InvoiceNO=@InvoiceNO ";
        }

        if (ViewState["Status"] != null)
        {
            list.Add(new SqlParameter("@Status", ViewState["Status"].ToString()));
            sqlstr += " and Status = @Status";
        }
        parms = list.ToArray();
    }

    private void dataBind()
    {
        if (Session["UserID"] != null)
        {
           
            string sqlStr = "SELECT * FROM BN_BookingNote where 1=1 ";
            SqlParameter[] parms = null;
            string clause = string.Empty;
            ProcessParms(ref clause, ref parms);
            sqlStr += clause;

            DataTable dt = bookNoteManage.GetBookingNoteList(sqlStr, parms);
            DataView view = dt.DefaultView;
            view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
            gvBookNoteList.DataSource = view;
            gvBookNoteList.DataBind();
            if (dt != null && dt.Rows.Count > 0)
            {
                lblTotal.Text = dt.Rows.Count.ToString();
            }
            else
            {
                lblTotal.Text = "0";
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtSelPONO.Text != string.Empty)
        {
            ViewState["PONO"] = txtSelPONO.Text.Trim();
        }
        else
        {
            ViewState["PONO"] = null;
        }

       
        if (txtInvoiceNO.Text != string.Empty)
        {
            ViewState["InvoiceNO"] = txtInvoiceNO.Text.Trim();
        }
        else
        {
            ViewState["InvoiceNO"] = null;
        }

        if (radAll.Checked)
        {
            ViewState["Status"] = null;
        }
        else if (radInput.Checked)
        {
            ViewState["Status"] = Enums.BookingNoteStatus.NEW.ToString("d");
        }
        else if (radCheck.Checked)
        {
            ViewState["Status"] = Enums.BookingNoteStatus.CHECK.ToString("d");
        }
        else
        {
            ViewState["Status"] = Enums.BookingNoteStatus.CANCEL.ToString("d");

        }
        dataBind();
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvBookNoteList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvBookNoteList.PageCount)
            {
                this.gvBookNoteList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvBookNoteList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvBookNoteList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvBookNoteList.PageIndex = e.NewPageIndex;
        dataBind();
    }

    protected void gvBookNoteList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }
}
